//
//  AppDelegate.m
//  SugarPile
//
//  Created by Charlie. on 16/1/18.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "HotViewController.h"
#import "FindViewController.h"
#import "StoreViewController.h"
#import "ClubViewController.h"
#import "LeadViewController.h"
#import "MyViewController.h"
#import "DataBaseManager.h"
#import "History.h"
#import "Movie.h"
@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate

-(void)dealloc
{
    [self.window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // 启动页延时
    [NSThread sleepForTimeInterval:1.0];
    
    DataBaseManager *dbManger = [DataBaseManager shareInstance];
    [dbManger openDB2];
    [dbManger openDB];
    [[DataBaseManager shareInstance] createTabel];
    [[DataBaseManager shareInstance] HistoryTabel];
    
    BOOL isFirst = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
    
    if (!isFirst) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        LeadViewController *yindaoyeVC = [[LeadViewController alloc] init];
        
        //TabBar
        self.tabBar = [[UITabBarController alloc] init];
        
        // Hot
        HotViewController *hotVC = [[HotViewController alloc] init];
        UINavigationController *hotNC = [[UINavigationController alloc] initWithRootViewController:hotVC];
        hotVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"shouye"] tag:10000];
        // Find
        
        FindViewController *findVC = [[FindViewController alloc] init];
        UINavigationController *findNC = [[UINavigationController alloc] initWithRootViewController:findVC];
        findVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"sousuo"] tag:10001] autorelease];
        
        // Store
        StoreViewController *StoreVC = [[StoreViewController alloc] init];
        UINavigationController *StoreNC = [[UINavigationController alloc] initWithRootViewController:StoreVC];
        StoreVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"商店" image:[UIImage imageNamed:@"shangdian"] tag:10002] autorelease];
        
        // Club
        
        ClubViewController *clubVC = [[ClubViewController alloc] init];
        UINavigationController *clubNC = [[UINavigationController alloc] initWithRootViewController:clubVC];
        clubVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"CLUB" image:[UIImage imageNamed:@"club"] tag:10003] autorelease];
        
        // self
        MyViewController *selfVC = [[MyViewController alloc] init];
        UINavigationController *selfNC = [[UINavigationController alloc] initWithRootViewController:selfVC];
        selfVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"yonghu"] tag:10004] autorelease];
        
        // 加入TabBar
        _tabBar.tabBar.tintColor = [UIColor redColor];
        _tabBar.delegate = self;
        _tabBar.viewControllers = @[hotNC, findNC, StoreNC, clubNC, selfNC];
        self.window.rootViewController = yindaoyeVC;
    }else{
    //TabBar
    self.tabBar = [[UITabBarController alloc] init];
    
    // Hot
    HotViewController *hotVC = [[HotViewController alloc] init];
    UINavigationController *hotNC = [[UINavigationController alloc] initWithRootViewController:hotVC];
    hotVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"shouye"] tag:10000];
    // Find
    
    FindViewController *findVC = [[FindViewController alloc] init];
    UINavigationController *findNC = [[UINavigationController alloc] initWithRootViewController:findVC];
    findVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"sousuo"] tag:10001] autorelease];
    
    // Store
    StoreViewController *StoreVC = [[StoreViewController alloc] init];
    UINavigationController *StoreNC = [[UINavigationController alloc] initWithRootViewController:StoreVC];
    StoreVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"商店" image:[UIImage imageNamed:@"shangdian"] tag:10002] autorelease];
    
    // Club
    
    ClubViewController *clubVC = [[ClubViewController alloc] init];
    UINavigationController *clubNC = [[UINavigationController alloc] initWithRootViewController:clubVC];
    clubVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"CLUB" image:[UIImage imageNamed:@"club"] tag:10003] autorelease];
    
    // self
    MyViewController *selfVC = [[MyViewController alloc] init];
    UINavigationController *selfNC = [[UINavigationController alloc] initWithRootViewController:selfVC];
    selfVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"yonghu"] tag:10004] autorelease];
    
    // 加入TabBar
    _tabBar.tabBar.tintColor = [UIColor redColor];
    _tabBar.delegate = self;
    _tabBar.viewControllers = @[hotNC, findNC, StoreNC, clubNC, selfNC];
    self.window.rootViewController = _tabBar;
    
    [_tabBar release];
    [findVC release];
    [findNC release];
    [StoreVC release];
    [StoreNC release];
    [clubVC release];
    [clubNC release];
    [selfVC release];
    [selfNC release];
        [self.window release];
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
